<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="80px" inline>
      <el-form-item label="角色名称">
        <el-input style="width: 80%;" v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit(form)">查询</el-button>
        <el-button @click="onReset(form)">重置</el-button>
        <el-button type="success" @click="showDia('', 'add')">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        type="index"
        width="30">
      </el-table-column>
      <el-table-column align="center" label="角色名称" width="95">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间" width="170">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column label="更新时间" width="170" align="center">
        <template slot-scope="scope">
          {{ scope.row.updateTime }}
        </template>
      </el-table-column>
      <el-table-column label="最后更新用户" width="130" align="center">
        <template slot-scope="scope">
          {{ scope.row.updateBy }}
        </template>
      </el-table-column>
      <el-table-column label="描述信息" width="200" align="center">
        <template slot-scope="scope">
          {{ scope.row.description }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="350">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="showDia(scope.row.id, 'look')">查看</el-button>
          <el-button
            size="mini"
            @click="showDia(scope.row.id, 'edit')">编辑
          </el-button>
          <el-popconfirm title="确定删除吗？" @onConfirm="handleDelete(scope.row.id)">
            <el-button size="mini" type="danger" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="margin-top: 40px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="100"
        layout="prev, pager, next, jumper"
        :total="1000">
      </el-pagination>
    </div>
    <el-dialog title="详情" :visible.sync="dialogFormVisible">
      <el-form ref="roleDetail" label-width="80px" :disabled="diaLook" inline>
        <el-form-item label="角色名称">
          <el-input style="width: 80%;" v-model="roleDetail.name"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input style="width: 80%;" v-model="roleDetail.description"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="onCancle">取 消</el-button>
        <el-button type="primary" @click="onUpdate(roleDetail)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { deleteRoleById, getAllRoleList, getRoleById, addRole, updateRole } from '@/api/role'
``
export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: false,
      form: {},
      roleDetail: {},
      currentPage: 1,
      dialogFormVisible: false,
      diaLook: true,
      operate: '',
      roleOptions: [],
      statusOptions: [{
        value: 0,
        label: '冻结'
      }, {
        value: 1,
        label: '正常'
      }],
      genderOptions: [{
        value: '男',
        label: '男'
      }, {
        value: '女',
        label: '女'
      }]
    }
  },
  created() {
    this.fetchData(this.form)
    this.allRoleList()
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.form.pageNum = val
      getAllRoleList(this.form).then(res => {
        this.list = res.data.rows
      })
    },
    handleEdit(row) {
    },
    showDia(id, operate) {
      this.operate = operate
      if (operate === 'look') {
        getRoleById(id).then(res => {
          this.roleDetail = res.data
          this.diaLook = true
        })
      } else if (operate === 'edit') {
        getRoleById(id).then(res => {
          this.roleDetail = res.data
          this.diaLook = false
        })
      } else if (operate === 'add') {
        this.roleDetail = {}
        this.diaLook = false
      }
      this.dialogFormVisible = true
    },
    handleDelete(id) {
      deleteRoleById(id).then(res => {
        this.onSubmit(this.form)
      })
    },
    onSubmit(param) {
      getAllRoleList(param).then(res => {
        this.list = res.data.rows
      })
      // this.fetchData(this.form)
    },
    onReset(form) {
      this.form = {}
    },
    onCancle() {
      this.dialogFormVisible = false
      this.roleDetail = {}
    },
    onUpdate(data) {
      if (this.diaLook === true) {
        this.dialogFormVisible = false
        this.roleDetail = {}
        return
      }
      if (this.operate === 'add') {
        addRole(data).then(res => {
          this.dialogFormVisible = false
          this.onSubmit(this.form)
          this.equipmentDetail = {}
        })
      } else if (this.operate === 'edit') {
        updateRole(data).then(res => {
          this.dialogFormVisible = false
          this.onSubmit(this.form)
          this.roleDetail = {}
        })
      }
      this.operate = ''
    },
    fetchData(param) {
      getAllRoleList(param).then(res => {
        this.list = res.data.rows
      })
    }
  }
}
</script>
